//
//  RelatedVideosTableViewCell.m
//  CapriceMusic
//
//  Created by 蒋恩浩 on 16/1/8.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "RelatedVideosTableViewCell.h"
#import "DownLoadManager.h"
@interface RelatedVideosTableViewCell ()<UIActionSheetDelegate>

@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *artistNameLabel;
@property (nonatomic, strong) UILabel *playCountLabel;
@property (nonatomic, strong) UIButton *moreButton;

@end

@implementation RelatedVideosTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.pictureView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.pictureView.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
        self.pictureView.layer.masksToBounds = YES;
        self.pictureView.layer.cornerRadius = 10;
        self.titleLabel = [[JEHLabel alloc] initWithFrame:CGRectZero withFont:14 withTextColor:[UIColor whiteColor]];
        self.artistNameLabel = [[JEHLabel alloc] initWithFrame:CGRectZero withFont:14 withTextColor:[UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0]];
        self.playCountLabel = [[JEHLabel alloc] initWithFrame:CGRectZero withFont:12 withTextColor:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0]];
        self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.moreButton setImage:[UIImage imageNamed:@"whitePoint"] forState:UIControlStateNormal];
        [self.moreButton addTarget:self action:@selector(moreButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.moreButton];
        [self addSubview:self.pictureView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.artistNameLabel];
        [self addSubview:self.playCountLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.pictureView.frame = CGRectMake(5, 5, (self.width - 20) * 2 / 5, self.height - 10);
    self.titleLabel.frame = CGRectMake(self.pictureView.width + 20, 10, self.width - self.pictureView.width - 40 - 50, (self.height - 20) / 3);
    self.artistNameLabel.frame = CGRectMake(self.pictureView.width + 20, self.titleLabel.height + 10, self.titleLabel.width, (self.height - 20) / 3);
    self.playCountLabel.frame = CGRectMake(self.pictureView.width + 20, self.titleLabel.height * 2 + 10, self.titleLabel.width, (self.height - 20) / 3);
    self.moreButton.frame = CGRectMake(self.width - 60, self.height / 2 - 15, 50, 30);
}

- (void)setRelatedVideoModel:(RelatedVideosModel *)relatedVideoModel {
    
    if (_relatedVideoModel != relatedVideoModel) {
        
        _relatedVideoModel = relatedVideoModel;
    }
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:relatedVideoModel.posterPic]];
    self.titleLabel.text = relatedVideoModel.title;
    self.artistNameLabel.text = relatedVideoModel.artistName;
    self.playCountLabel.text = [NSString stringWithFormat:@"播放次数:%d", relatedVideoModel.totalViews.intValue];
}

- (void)setIdType:(NSString *)idType {
    
    if (_idType != idType) {
        
        _idType = idType;
    }
}

- (void)moreButtonAction {
    
    // 详情选项
    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"下载", @"收藏", nil];
    actionsheet.actionSheetStyle = UIActionSheetStyleAutomatic;
    [actionsheet showInView:self];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        DownLoadManager *down = [DownLoadManager shareDownLoadManager];
        if (down.isdownloading == YES) {
            UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在当前任务结束后重试" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [view show];
        } else {
            [down downLoad:self.relatedVideoModel.url];
            DataBaseHandle *data = [DataBaseHandle shareDataBase];
            [data openDataBase];
            [data createDownloadingTable];
            [data insertDownloadingDatabase:self.relatedVideoModel.title withPicture:self.relatedVideoModel.posterPic withDownloadUrl:self.relatedVideoModel.url];
            [data closedDataBase];
            
        }
    }else if (buttonIndex == 1) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        BOOL mybool = YES;
        DataBaseHandle *dataBaseHandle = [DataBaseHandle shareDataBase];
        [dataBaseHandle openDataBase];
        [dataBaseHandle createTable];
        NSMutableArray *array = [dataBaseHandle selectDataBase];
        if (array.count == 0) {
            [dataBaseHandle insertDatabase:self.relatedVideoModel.title withids:self.relatedVideoModel.ids.stringValue withType:self.idType withImageUrl:self.relatedVideoModel.albumImg];
            
            [center postNotificationName:@"collect" object:@"收藏成功" userInfo:nil];
        }else {
            for (NSDictionary *dic in array) {
                if ([[dic valueForKey:@"ids"] isEqualToString:self.relatedVideoModel.ids.stringValue]) {
                    [center postNotificationName:@"collect" object:@"亲,您已经收藏过了" userInfo:nil];
                    mybool = NO;
                    break;
                }
            }
            if (mybool) {
                [dataBaseHandle insertDatabase:self.relatedVideoModel.title withids:self.relatedVideoModel.ids.stringValue withType:self.idType withImageUrl:self.relatedVideoModel.albumImg];
                [center postNotificationName:@"collect" object:@"收藏成功" userInfo:nil];
            }
            [dataBaseHandle closedDataBase];
        }
    }
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {
    
    NSLog(@"actionSheetCancel");
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    //    NSLog(@"didDismiss");
}

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
    //    NSLog(@"willDismiss");
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end














